using System.Collections.Generic;
using UnityEngine;

public class AIPerception : Perception
{
	private List<Perception> enemies;

	private List<Wall> walls;

	private List<Collectible> collectibles;

	private Vector2 displacement;

	private Character curTarget;

	private Character nextTarget;

	private AICharacter aiCharacter;

	private float trackUntil;

	private Collider2D[] cols = new Collider2D[0];

	private void Awake()
	{
		enemies = new List<Perception>();
		walls = new List<Wall>();
		collectibles = new List<Collectible>();
		aiCharacter = character.GetComponent<AICharacter>();
	}

	private void OnEnable()
	{
		enemies.Clear();
		walls.Clear();
		collectibles.Clear();
		curTarget = null;
		nextTarget = null;
	}

	public Vector2 GetDirection()
	{
		cols = Physics2D.OverlapCircleAll(character.Position, 2.5f, 9728, 0f, 0f);
		if (cols != null)
		{
			enemies.Clear();
			walls.Clear();
			collectibles.Clear();
			for (int i = 0; i < cols.Length; i++)
			{
				if (cols[i].CompareTag(Values.Instance.PerceptionTag))
				{
					enemies.Add(cols[i].GetComponent<Perception>());
				}
				else if (cols[i].CompareTag(Values.Instance.WallTag))
				{
					walls.Add(cols[i].GetComponent<Wall>());
				}
				else if (cols[i].CompareTag(Values.Instance.CollectibleTag))
				{
					collectibles.Add(cols[i].GetComponent<Collectible>());
				}
			}
		}
		Vector3 zero = Vector3.zero;
		Vector2 zero2 = Vector2.zero;
		bool flag = false;
		bool flag2 = false;
		if (!aiCharacter.IsHunter || !GameManager.Instance.PlayerCharacter.IsAlive || GameManager.Instance.PlayerCharacter.Rank != 1)
		{
			for (int j = 0; j < enemies.Count; j++)
			{
				if (character.Mass <= 12)
				{
					zero2 += Steering.Flee(character.MoveDirection, enemies[j].character.Position, character.Position, character.Speed) * 1.1f;
					flag = true;
				}
				else if ((float)enemies[j].character.Mass * 0.3f > (float)character.Mass)
				{
					zero2 += Steering.Flee(character.MoveDirection, enemies[j].character.Position, character.Position, character.Speed) * 1.1f;
					flag = true;
				}
				else
				{
					if (enemies[j].character.Mass >= character.Mass || (!(enemies[j].character != GameManager.Instance.PlayerCharacter) && (!(enemies[j].character == GameManager.Instance.PlayerCharacter) || enemies[j].character.Mass <= 200)))
					{
						continue;
					}
					flag2 = true;
					if (nextTarget != null)
					{
						if (nextTarget.Mass > enemies[j].character.Mass)
						{
							nextTarget = enemies[j].character;
						}
					}
					else
					{
						nextTarget = enemies[j].character;
					}
				}
			}
		}
		else
		{
			flag2 = true;
			nextTarget = GameManager.Instance.PlayerCharacter;
		}
		if (flag2)
		{
			if (curTarget != nextTarget)
			{
				curTarget = nextTarget;
				trackUntil = Time.time + Random.Range(2f, 6f);
				zero2 += Steering.Seek(character.MoveDirection, curTarget.Position, character.Position, character.Speed);
				if (!aiCharacter.BoostEnabled && trackUntil - Time.time > 4.5f)
				{
					aiCharacter.OpenBoost();
				}
			}
			else if (Time.time < trackUntil)
			{
				zero2 += Steering.Seek(character.MoveDirection, curTarget.Position, character.Position, character.Speed);
			}
			else
			{
				zero2 += Steering.Wander(character.MoveDirection, character.Position, character.Speed, 1f, 1f, 90f, ref displacement);
			}
		}
		else
		{
			curTarget = null;
			nextTarget = null;
		}
		for (int k = 0; k < walls.Count; k++)
		{
			zero2 += Steering.Flee(character.MoveDirection, walls[k].transform.position, character.Position, character.Speed) * 0.45f;
			flag = true;
		}
		if (!flag)
		{
			zero2 += Steering.RunAwayFromBorder(character.MoveDirection, character.Position, character.Speed) * 3f;
		}
		if (!flag && collectibles.Count > 4)
		{
			Vector2 zero3 = Vector2.zero;
			int num = 0;
			for (int l = 0; l < collectibles.Count; l++)
			{
				float distance = GetDistance(collectibles[l].Position, base.transform.position);
				if (distance > 2.5f)
				{
					collectibles.RemoveAt(l);
					l--;
				}
				else
				{
					zero3 += collectibles[l].Mass * collectibles[l].Position;
					num += collectibles[l].Mass;
				}
			}
			if (collectibles.Count > 4)
			{
				flag2 = true;
				zero3 /= (float)num;
				zero2 += Steering.Seek(character.MoveDirection, zero3, character.Position, character.Speed) * 0.5f;
			}
		}
		if (!flag || !flag2)
		{
			zero2 += Steering.Wander(character.MoveDirection, character.Position, character.Speed, 1f, 1f, 90f, ref displacement);
		}
		return zero2 * 0.14f;
	}

	private float GetDistance(Vector3 p1, Vector3 p2)
	{
		return Vector2.Distance(p1, p2);
	}
}
